/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__frct_set_l4_transaction.c                       */
/*  NOTE      =                                                    */
/*  DATE      = 2014/12/17 by geyh                                 */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>
#include <time.h>

#include "frcomm.h"
#include "frlog.h"
#include "frmem.h"
#include "frlist.h"
#include "frsutl.h"
#include "frhash2.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frct_mng.h"
#include "apmtest_cunit.h"

static int __suite_init(void)
{
	return 0;
}

static int __suite_cleanup(void)
{
	return 0;
}
void __frct_set_l4_transaction(Conntrack *p_st_ct
                                    , u_int ui_src, u_short us_sport
                                    , unsigned int ui_data_len
                                    , struct timeval st_timeval_pkg);

static void __case1(void)
{
    Conntrack *p_st_ct = NULL;
    u_int ui_src= iptools_parse_ipaddr((char *)"192.168.1.1");
    u_short us_sport = 80;
    unsigned int ui_data_len = 10;
    struct timeval st_timeval_pkg;

    st_timeval_pkg.tv_sec = 100;
    st_timeval_pkg.tv_usec = 0;

    __frct_set_l4_transaction(p_st_ct,ui_src, us_sport, ui_data_len, st_timeval_pkg);

    return;
}

static void __case2(void)
{
    Conntrack *p_st_ct = NULL;
    u_int ui_src= iptools_parse_ipaddr((char *)"192.168.1.1");
    u_short us_sport = 80;
    unsigned int ui_data_len = 10;
    struct timeval st_timeval_pkg;

    st_timeval_pkg.tv_sec = 100;
    st_timeval_pkg.tv_usec = 0;

    p_st_ct = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_ct, 0x00, sizeof(Conntrack));


    __frct_set_l4_transaction(p_st_ct,ui_src, us_sport, ui_data_len, st_timeval_pkg);

    frfree(p_st_ct);
    return;
}

static void __case3(void)
{
    Frlist_node *p_st_node = NULL;
    Conntrack *p_st_ct = NULL;
    L7_analyzer *p_st_L7_analyzer = NULL;
    L7_module *p_st_l7_module = NULL;
    L7_session      *p_st_session = NULL;
    L7_transaction  *p_st_transaction = NULL;
    char *p_tmp = NULL;
    u_int ui_src= iptools_parse_ipaddr((char *)"192.168.1.1");
    u_short us_sport = 80;
    unsigned int ui_data_len = 10;
    struct timeval st_timeval_pkg;

    st_timeval_pkg.tv_sec = 100;
    st_timeval_pkg.tv_usec = 0;
    //设置p_st_l7_module
    p_st_l7_module = (L7_module *)frmalloc(sizeof(L7_module));
    memset(p_st_l7_module, 0x00, sizeof(L7_module));
    p_st_l7_module->i_l7_proto = 1;
    
    p_tmp = (char *)frmalloc(strlen("module_name") + 1);
    snprintf(p_tmp,strlen("module_name") + 1,"%s","module_name");
    p_st_l7_module->p_module_name = p_tmp;
    p_tmp = (char *)frmalloc(strlen("version") + 1);
    snprintf(p_tmp,strlen("version") + 1,"%s","version");
    p_st_l7_module->p_version = p_tmp;

    //设置p_st_session
    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_tmp = (char *)frmalloc(strlen("username") + 1);
    snprintf(p_tmp,strlen("username") + 1,"%s","username");
    p_st_session->p_username = p_tmp;

    p_st_session->i_status = APM_SESSION_START;

    //p_st_transaction
    p_st_transaction = (L7_transaction *)frmalloc(sizeof(L7_transaction));
    memset(p_st_transaction, 0x00, sizeof(L7_transaction));
    p_st_transaction->i_status = 1;

    p_tmp = (char *)frmalloc(strlen("transaction_code") + 1);
    snprintf(p_tmp,strlen("transaction_code") + 1,"%s","transaction_code");
    p_st_transaction->p_transaction_code = p_tmp;

    p_st_transaction->p_transaction_title = NULL;

    p_tmp = (char *)frmalloc(strlen("server_name") + 1);
    snprintf(p_tmp,strlen("server_name") + 1,"%s","server_name");
    p_st_transaction->p_server_name = p_tmp;

    //设置p_st_L7_analyzer
    p_st_L7_analyzer = (L7_analyzer *)frmalloc(sizeof(L7_analyzer));
    memset(p_st_L7_analyzer, 0x00, sizeof(L7_analyzer));
    p_st_L7_analyzer->p_st_l7_module = p_st_l7_module;
    p_st_L7_analyzer->p_st_session = p_st_session;
    p_st_L7_analyzer->p_st_transaction = p_st_transaction;

    //设置p_st_ct
    p_st_ct = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_ct, 0x00, sizeof(Conntrack));
    p_st_ct->refcnt = 1;

    p_st_ct->src_ip = iptools_parse_ipaddr((char *)"192.168.1.1");
    p_st_ct->src_port = 80;
    p_st_ct->dst_ip = iptools_parse_ipaddr((char *)"192.168.0.1");
    p_st_ct->dst_port = 22;
    p_st_ct->ip_proto = 6;

    __frct_set_l4_transaction(p_st_ct,ui_src, us_sport, ui_data_len, st_timeval_pkg);

    //确认返回值
    CU_ASSERT_EQUAL(p_st_ct->st_l4_transaction.st_request_time.tv_sec,100);
    CU_ASSERT_EQUAL(p_st_ct->st_l4_transaction.st_request_time.tv_usec,0);
    CU_ASSERT_EQUAL(p_st_ct->st_l4_transaction.st_response_time_first_byte.tv_sec,0);
    CU_ASSERT_EQUAL(p_st_ct->st_l4_transaction.st_response_time_first_byte.tv_usec,0);

    frfree(p_st_l7_module->p_module_name);
    frfree(p_st_l7_module->p_version);
    frfree(p_st_l7_module);

    frfree(p_st_transaction->p_server_name);
    frfree(p_st_transaction->p_transaction_code);
    frfree(p_st_transaction);
    frfree(p_st_session->p_username);
    frfree(p_st_session);
    frfree(p_st_L7_analyzer);
    frfree(p_st_ct);

}

static void __case4(void)
{
    Frlist_node *p_st_node = NULL;
    Conntrack *p_st_ct = NULL;
    L7_analyzer *p_st_L7_analyzer = NULL;
    L7_module *p_st_l7_module = NULL;
    L7_session      *p_st_session = NULL;
    L7_transaction  *p_st_transaction = NULL;
    char *p_tmp = NULL;
    u_int ui_src= iptools_parse_ipaddr((char *)"192.168.0.1");
    u_short us_sport = 22;
    unsigned int ui_data_len = 10;
    struct timeval st_timeval_pkg;

    st_timeval_pkg.tv_sec = 100;
    st_timeval_pkg.tv_usec = 0;
    //设置p_st_l7_module
    p_st_l7_module = (L7_module *)frmalloc(sizeof(L7_module));
    memset(p_st_l7_module, 0x00, sizeof(L7_module));
    p_st_l7_module->i_l7_proto = 1;
    p_tmp = (char *)frmalloc(strlen("module_name") + 1);
    snprintf(p_tmp,strlen("module_name") + 1,"%s","module_name");
    p_st_l7_module->p_module_name = p_tmp;
    p_tmp = (char *)frmalloc(strlen("version") + 1);
    snprintf(p_tmp,strlen("version") + 1,"%s","version");
    p_st_l7_module->p_version = p_tmp;

    //设置p_st_session
    p_st_session = (L7_session *)frmalloc(sizeof(L7_session));
    memset(p_st_session, 0x00, sizeof(L7_session));
    p_tmp = (char *)frmalloc(strlen("username") + 1);
    snprintf(p_tmp,strlen("username") + 1,"%s","username");
    p_st_session->p_username = p_tmp;

    p_st_session->i_status = APM_SESSION_START;

    //p_st_transaction
    p_st_transaction = (L7_transaction *)frmalloc(sizeof(L7_transaction));
    memset(p_st_transaction, 0x00, sizeof(L7_transaction));
    p_st_transaction->i_status = 1;

    p_tmp = (char *)frmalloc(strlen("transaction_code") + 1);
    snprintf(p_tmp,strlen("transaction_code") + 1,"%s","transaction_code");
    p_st_transaction->p_transaction_code = p_tmp;

    p_st_transaction->p_transaction_title = NULL;

    p_tmp = (char *)frmalloc(strlen("server_name") + 1);
    snprintf(p_tmp,strlen("server_name") + 1,"%s","server_name");
    p_st_transaction->p_server_name = p_tmp;

    //设置p_st_L7_analyzer
    p_st_L7_analyzer = (L7_analyzer *)frmalloc(sizeof(L7_analyzer));
    memset(p_st_L7_analyzer, 0x00, sizeof(L7_analyzer));
    p_st_L7_analyzer->p_st_l7_module = p_st_l7_module;
    p_st_L7_analyzer->p_st_session = p_st_session;
    p_st_L7_analyzer->p_st_transaction = p_st_transaction;


    //设置p_st_ct
    p_st_ct = (Conntrack *)frmalloc(sizeof(Conntrack));
    memset(p_st_ct, 0x00, sizeof(Conntrack));
    p_st_ct->refcnt = 1;

    p_st_ct->src_ip = iptools_parse_ipaddr((char *)"192.168.1.1");
    p_st_ct->src_port = 80;
    p_st_ct->dst_ip = iptools_parse_ipaddr((char *)"192.168.0.1");
    p_st_ct->dst_port = 22;
    p_st_ct->ip_proto = 6;

    __frct_set_l4_transaction(p_st_ct,ui_src, us_sport, ui_data_len, st_timeval_pkg);

    //确认返回值
    CU_ASSERT_EQUAL(p_st_ct->st_l4_transaction.st_response_time_first_byte.tv_sec,100);
    CU_ASSERT_EQUAL(p_st_ct->st_l4_transaction.st_response_time_first_byte.tv_usec,0);
    CU_ASSERT_EQUAL(p_st_ct->st_l4_transaction.st_response_time.tv_sec,100);
    CU_ASSERT_EQUAL(p_st_ct->st_l4_transaction.st_response_time.tv_usec,0);



    frfree(p_st_l7_module->p_module_name);
    frfree(p_st_l7_module->p_version);
    frfree(p_st_l7_module);

    frfree(p_st_transaction->p_server_name);
    frfree(p_st_transaction->p_transaction_code);
    frfree(p_st_transaction);
    frfree(p_st_session->p_username);
    frfree(p_st_session);
    frfree(p_st_L7_analyzer);
    frfree(p_st_ct);

}


int add_suite()
{
	CU_pSuite pSuite = NULL;

	pSuite = CU_add_suite("t__frct_set_l4_transaction 测试", __suite_init, __suite_cleanup);

	if (NULL == pSuite)
	{
		return 1;
	}


    //增加测试用例
   	CU_add_test(pSuite, "case1", __case1);

   	CU_add_test(pSuite, "case2", __case2);

   	CU_add_test(pSuite, "case3", __case3);

   	CU_add_test(pSuite, "case4", __case4);

    return 0;

}

int main(int argc, char **argv)
{
    fr_slab_init(52428800, "test");
    run_cunit(add_suite);
    fr_slab_destroy();

    return 0;
}